package com.my.baidu.IPdingwei.api;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Auther: szh
 * @Date:2021-06-23 11:12
 */

public class IPDingwei {

    private static final String accessKey="2c34c38899de491094a796a6b1593061";

    private static final String secretKey="56d508f1a33642bfa7ddf660140b9715";


    public Map<String, String> getWeiZhi(String ip){
        String path = "http://gwgp-hrtx4zoqeyk.n.bdcloudapi.com/iplocaltion";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.GET, path);
        request.setCredentials(accessKey, secretKey);
        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
        request.addQueryParameter("ip", ip);
        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
            JSONObject json = JSONObject.fromObject(response.getResult());
            String result = json.getString("result");
            JSONObject jsonObject = JSONObject.fromObject(result);
            String lat = jsonObject.getString("lat");
            String lng = jsonObject.getString("lng");
            Map<String, String> map = new HashMap<String, String>();
            map.put("lat",lat);
            map.put("lng",lng);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
